package com.wangxiang.shop.member.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangxiang.common.utils.*;
import com.wangxiang.shop.member.dao.MemberLocationDao;
import com.wangxiang.shop.member.dao.ReceiptDao;
import com.wangxiang.shop.member.entity.MemberLocationEntity;
import com.wangxiang.shop.member.entity.ReceiptEntity;
import com.wangxiang.shop.member.entity.ShopCartEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wangxiang.shop.member.dao.MemberDao;
import com.wangxiang.shop.member.entity.MemberEntity;
import com.wangxiang.shop.member.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;


@Service("shopUserService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private ReceiptDao receiptDao;
    @Autowired
    private MemberLocationDao locationDao;


    @Override
    public int register(HashMap<String , Object> map) {
        String nullStr = "";
      //表单校验完成,插入操作
        MemberEntity memberEntity = new MemberEntity();
        if (map.get("openId")!=null){
            //可以为空
            memberEntity.setOpenId(map.get("openId").toString());
        }

        if (map.get("name")!=null|!nullStr.equals(map.get("name"))){
            memberEntity.setName(map.get("name").toString());
        }
        //昵称为空的情况，使用登录名作为昵称
        if (map.get("name")==null|nullStr.equals(map.get("name"))){
            memberEntity.setName(map.get("loginName").toString());
        }
        if (map.get("mobail")!=null|!nullStr.equals(map.get("mobail"))){
            memberEntity.setMobail(map.get("mobail").toString());
        }
        if (map.get("image")==null|nullStr.equals(map.get("image"))){
            //默认的头像
            memberEntity.setImage("https://shop-wangxiang.oss-cn-chengdu.aliyuncs.com/userImage/userImage.png");
        }
        if (map.get("image")!=null){
            memberEntity.setImage(map.get("image").toString());
        }
        memberEntity.setEmail(map.get("email").toString());
        memberEntity.setLoginName(map.get("loginName").toString());
        memberEntity.setPassword(map.get("password").toString());
        int payPassword = Integer.valueOf(map.get("payPassword").toString());
        memberEntity.setPayPassword(payPassword);
        int set = Integer.valueOf(map.get("sex").toString());
        memberEntity.setSex(set);
        int age = Integer.valueOf(map.get("age").toString());
        memberEntity.setAge(age);
        //余额初始10万，虚拟金额
        memberEntity.setBalance(100000.0);
        memberEntity.setStatus(0); //状态
        System.out.println("实体"+memberEntity);
        int insert = memberDao.insert(memberEntity);
        return insert;





    }

    @Override
    public MemberEntity selectUserByLoginName(String name) {
        MemberEntity memberByLoginNmae = memberDao.getMemberByLoginNmae(name);
        return memberByLoginNmae;
    }

    @Override
    public ShopCartEntity list(int userId) {

        return null;
    }

    @Override
    public MemberEntity info(int userId) {
        MemberEntity member = memberDao.selectById(userId);
        return member;
    }

    @Override
    public PageEntity page(PageEntity pageEntity) {
        PageHelper.startPage(pageEntity.getPageNum(),pageEntity.getPageSize());
        List<MemberEntity> memberEntities = memberDao.selectList(null);
        PageInfo<MemberEntity> memberEntityPageInfo = new PageInfo<>(memberEntities);
        PageGenerate pageGenerate = new PageGenerate();
        PageEntity generate = pageGenerate.generate(memberEntityPageInfo);
        return generate;
    }

    @Override
    public int balance(int userId, double price) {
        memberDao.updateBalance(userId,price);
        return 0;
    }

    @Override
    public MemberEntity getEntityByName(String name) {
        List<MemberEntity> memberEntities = memberDao.selectList(null);
        ArrayList<MemberEntity> list = new ArrayList<>();
        for (MemberEntity memberEntity : memberEntities) {
            if (!list.contains(memberEntity)){
                if (name.equals(memberEntity.getName())){
                    list.add(memberEntity);
                }
            }
        }
        if (list.size()==0){
            return null;
        }
        return list.get(0);
    }

    @Override
    public MemberEntity selectEntityByName(String name) {
        MemberEntity memberEntity = memberDao.selectByName(name);
        return memberEntity;
    }

    @Override
    public int updateStatus(HashMap<String, Object> map) {
        int id = Integer.valueOf(map.get("id").toString());
        int remark = memberDao.updateStatus(id, 1, map.get("remark").toString());

        return remark;
    }

    @Override
    public List<HashMap<String, Object>> memberOrderList(List<HashMap<String, Object>> list) {
       //查用户名,收货人，收货地址，收货电话
        list.forEach((item)->{
            int userId = Integer.valueOf(item.get("userId").toString());
            int receipt = Integer.valueOf(item.get("receiptId").toString());
            MemberEntity memberEntity = memberDao.selectById(userId);
            ReceiptEntity receiptEntity = receiptDao.selectById(receipt);
            item.put("userName",memberEntity.getLoginName());
            item.put("shipName",receiptEntity.getName());
            item.put("shipMobail",receiptEntity.getMobail());
            item.put("shipLocation",receiptEntity.getProvince()+receiptEntity.getCity()+receiptEntity.getArea()+receiptEntity.getLocaltion());
        });

        return list;
    }

    @Override
    public List<MemberEntity> entityList() {
        List<MemberEntity> memberEntities = memberDao.selectList(null);
        return memberEntities;
    }

    @Override
    public List<DataEntity> sexData() {
        ArrayList<DataEntity> dataEntities = new ArrayList<>();
        List<MemberEntity> memberEntities = memberDao.selectList(null);
        for (int i = 0; i <=2; i++) {
            DataEntity dataEntity = new DataEntity();
            int sexCount = memberDao.getSexCount(i);
//            System.out.println(sexCount);
            if (i==0){
                dataEntity.setName("不愿透露");
            }
            if (i==1){
                dataEntity.setName("男");
            }
            if (i==2){
                dataEntity.setName("女");
            }
            dataEntity.setValue(sexCount);
            dataEntities.add(dataEntity);
        }
        return dataEntities;
    }

    @Override
    public List<DataEntity> ageData() {
        ArrayList<DataEntity> list = new ArrayList<>();
        String age[] = {"0~10","11~20","21~30","31~40","41~50","51~60","61~70","71~80","81~90","91~100"};
        for (int i = 0; i < age.length; i++) {
            DataEntity dataEntity = new DataEntity();
            if (i==0){
                int i1 = memberDao.ageCount(0, 10);
                dataEntity.setValue(i1);
                dataEntity.setName(age[i]);
                list.add(dataEntity);
            }
            if (i==1){
                int i1 = memberDao.ageCount(11, 20);
                dataEntity.setValue(i1);
                dataEntity.setName(age[i]);
                list.add(dataEntity);
            }
            if (i==2){
                int i1 = memberDao.ageCount(21, 30);
                dataEntity.setValue(i1);
                dataEntity.setName(age[i]);
                list.add(dataEntity);
            }
            if (i==3){
                int i1 = memberDao.ageCount(31, 40);
                dataEntity.setValue(i1);
                dataEntity.setName(age[i]);
                list.add(dataEntity);
            }

            if (i==4){
                int i1 = memberDao.ageCount(41, 50);
                dataEntity.setValue(i1);
                dataEntity.setName(age[i]);
                list.add(dataEntity);
            }
            if (i==5){
                int i1 = memberDao.ageCount(51, 60);
                dataEntity.setValue(i1);
                dataEntity.setName(age[i]);
                list.add(dataEntity);
            }
            if (i==6){
                int i1 = memberDao.ageCount(61, 70);
                dataEntity.setValue(i1);
                dataEntity.setName(age[i]);
                list.add(dataEntity);
            }
            if (i==7){
                int i1 = memberDao.ageCount(71, 80);
                dataEntity.setValue(i1);
                dataEntity.setName(age[i]);
                list.add(dataEntity);
            }
            if (i==8){
                int i1 = memberDao.ageCount(81, 90);
                dataEntity.setValue(i1);
                dataEntity.setName(age[i]);
                list.add(dataEntity);
            }
            if (i==9){
                int i1 = memberDao.ageCount(91, 100);
                dataEntity.setValue(i1);
                dataEntity.setName(age[i]);
                list.add(dataEntity);
            }

        }





        return list;
    }

    @Override
    public List<DataEntity> locationData() {
        List<MemberLocationEntity> memberLocationEntities = locationDao.selectList(null);
        ArrayList<DataEntity> list = new ArrayList<>();
        for (MemberLocationEntity memberLocationEntity : memberLocationEntities) {
            DataEntity dataEntity = new DataEntity();
            int locationCountByIndex = memberDao.getLocationCountByIndex(memberLocationEntity.getId());
            String city = memberLocationEntity.getName().replace("市", "");
            String province = city.replace("省", "");
            String s = province.replace("自治区", "");
            String s1 = s.replace("特别行政区", "");


            dataEntity.setName(s1);

            dataEntity.setValue(locationCountByIndex);
            list.add(dataEntity);
        }
        return list;


    }

    @Override
    public HashMap<String, Integer> numInfo() {
        List<MemberEntity> memberEntities = memberDao.selectList(null);


        int line = 0;
        int invalid = 0;
        for (MemberEntity memberEntity : memberEntities) {
            if (memberEntity.getLineStatus()==1){
                line++;
            }
            if(memberEntity.getStatus()==1){
                invalid++;
            }
        }
        HashMap<String, Integer> map = new HashMap<>();
        map.put("all",memberEntities.size());
        map.put("line",line);
        map.put("invalid",invalid);
        return map;
    }

    @Override
    public void line(int userId,int status) {
        memberDao.updateLine(userId,status);
    }

    //定时任务
    @Scheduled(cron = "0/30 * * * * ?")
    public void outLineTask(){
        //全部下线，更新一遍状态
        memberDao.updateOutLine();
//        System.out.println("定时任务清空状态");
    }
}